package easier.app.x.view;

import androidx.recyclerview.widget.DiffUtil;
import java.util.List;
import java.util.Objects;

/**
 * 计算数据差异,通知变化
 */
public class CommonDiff< T > extends DiffUtil.Callback {

    protected List< T > mOldList;
    protected List< T > mNewList;

    public CommonDiff() {

    }

    public CommonDiff( List< T > oldList, List< T > newList ) {

        mOldList = oldList;
        mNewList = newList;
    }

    public void setOldList( List< T > oldList ) {

        mOldList = oldList;
    }

    public void setNewList( List< T > newList ) {

        mNewList = newList;
    }

    @Override
    public int getOldListSize() {

        return mOldList == null ? 0 : mOldList.size();
    }

    @Override
    public int getNewListSize() {

        return mNewList == null ? 0 : mNewList.size();
    }

    @Override
    public boolean areItemsTheSame( int oldItemPosition, int newItemPosition ) {

        return Objects.equals( mOldList.get( oldItemPosition ), mNewList.get( newItemPosition ) );
    }

    @Override
    public boolean areContentsTheSame( int oldItemPosition, int newItemPosition ) {

        return Objects.equals( mOldList.get( oldItemPosition ), mNewList.get( newItemPosition ) );
    }
}
